<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Api\CommonController;
use App\Model\Userlibrary;
use Illuminate\Http\Request;
use Auth;

class EntryController extends CommonController
{
//    (1)登录处理
    public function login(Request $request)
    {
//        让登录处理去找admin模型来处理，且字段为username和pasword
        $status = Auth::guard('userlibrary')->attempt([
           'username' => $request->input('username'),
           'password' => $request->input('password'),
        ]);
        if ($status) {
            return 1;
        }
//        登录失败
        return 0;
    }

//    (2)用户信息查找
    public function register(Request $request)
    {
        $userlibraries = new Userlibrary;
//
        $username = $request->username;
        $password = $request->password;
        $phone_number = $request->phone_number;
        $email = $request->email;
        $identity = $request->identity;
        $Noaudit_original = $request->Noaudit_original;
        $original_book = $request->original_book;

//        若四个变量任何一个为空则执行
        if(empty($username) || empty($password) || empty($phone_number) || empty($email) || empty($identity)){
//            定义一个json类型的数据，先定义为字符串，再利用json_decode转成json格式
            $d = '{"error":"填写的信息不可为空","error_code": "100"}';
            $data = json_decode($d, true);

            return $data;
        } else {
            $userlibraries->username = $username;
            $userlibraries->password = bcrypt($password);//对前台过来的密码数据进行加密
            $userlibraries->phone_number = $phone_number;
            $userlibraries->email = $email;
            $userlibraries->identity = $identity;
            $userlibraries->Noaudit_original = $Noaudit_original;
            $userlibraries->original_book = $original_book;

            $userlibraries->save();

            return "注册成功";
        }
    }
}
